package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.repository.ChooseCarRepository
import com.cheyun.netsalev3.utils.LettersComparator
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.choosecar.ChooseCarActivity
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

class ChooseCarViewModel(var chooseCarRepository: ChooseCarRepository, var carBrandParam: CarBrandParam) : BaseViewModel() {
    //只选择品牌
    private var Isbrand = false
    //只选择到车系
    private var Isseries = false
    //
    private var level = 0; //0 resultcode:102, 1 resultcode:104 , 2 resultcode:105
    //品牌
    var dataList = MutableLiveData<List<AZItemEntity<CarPinPaParam>>>()
    //车系
    var cexiList = MutableLiveData<List<X>>()
    //车型
    var carXingList = MutableLiveData<List<X>>()

    private fun fillData(date: List<CarPinPaParam>): List<AZItemEntity<CarPinPaParam>> {
        val sortList = ArrayList<AZItemEntity<CarPinPaParam>>()
        for (i in 0 until date.size) {
            var aDate = date[i]
            val item = AZItemEntity<CarPinPaParam>(null, null)
            item.value = aDate
            //取字母
            val letters = aDate.letter
            // 正则表达式，判断首字母是否是英文字母
            if (letters.matches("[A-Z]".toRegex())) {
                item.sortLetters = letters.toUpperCase()
            } else {
                item.sortLetters = "★"
            }
            sortList.add(item)
        }
        return sortList

    }

    init {
        chooseCarRepository.getBran(this::setBranList)
    }

    private fun setBranList(list: ArrayList<CarPinPaParam>) {
        var loginParam = MySharedPreferences.getLoginData()
        loginParam?.channel?.apply {
            var carPinPaParam = CarPinPaParam(this.brand, "★", this.brandname,if(this.brandurl == null)"" else this.brandurl)
            carPinPaParam.IsTop = true
            list.add(carPinPaParam)
        }
        val dateList = fillData(list)
        Collections.sort(dateList, LettersComparator())
        dataList.postValue(dateList)
    }

    // 点击品牌
    fun onClickBrand(view: View, item: AZItemEntity<CarPinPaParam>) {
        item.value?.apply {
            carBrandParam.brand = this.id.toString()
            carBrandParam.brandname = this.title
        }
        dataList.value?.apply {
            for (i in this) {
                i.value?.apply {
                    this.IsSelect = item.value!!.id == this.id
                }

            }
        }
        //车系车型清空
        carBrandParam.series = ""
        carBrandParam.seriesname = ""
        carBrandParam.spec = ""
        carBrandParam.specname = ""

        if (Isbrand) {
            var context = view.context
            if (context is AppCompatActivity) {
                var intent = Intent()
                intent.putExtra("car", carBrandParam as Serializable)
                context.setResult(102, intent)
                context.finish()
            }
        } else {
            //获取车系
            item.value?.id?.apply {
                chooseCarRepository.getCar("3", this.toString(), this@ChooseCarViewModel::setChex)
            }
            var actvity = view.context as ChooseCarActivity
            actvity.binding.viewPager.currentItem = 1
        }

    }

    // 点击车系
    fun onClickCeXi(view: View, item: X) {
        carBrandParam.series = item.id.toString()
        carBrandParam.seriesname = item.title
        var tem = cexiList.value
        tem?.apply {
            for (i in this) {
                i.IsSelect = item.id == i.id
            }
        }
        cexiList.postValue(tem)
        //车型清空
        carBrandParam.spec = ""
        carBrandParam.specname = ""

        if (Isseries) {
            var context = view.context
            if (context is AppCompatActivity) {
                var intent = Intent()
                intent.putExtra("car", carBrandParam as Serializable)
                var resultCode = 102
                if(level == 1) resultCode = 104
                else if(level == 2)resultCode = 105
                context.setResult(resultCode, intent)
                context.finish()
            }
        } else {
            //获取车型
            item?.id?.apply {
                chooseCarRepository.getCar("5", this.toString(), this@ChooseCarViewModel::setChex)
            }
            var actvity = view.context as ChooseCarActivity
            actvity.binding.viewPager.currentItem = 2
        }
    }

    // 点击车型
    fun onClickCar(view: View, item: X) {
        carBrandParam.spec = item.id.toString()
        carBrandParam.specname = item.title
        var tem = carXingList.value
        tem?.apply {
            for (i in this) {
                i.IsSelect = item.id == i.id
            }
        }
        carXingList.postValue(tem)
        var context = view.context
        if (context is AppCompatActivity) {
            var intent = Intent()
            intent.putExtra("car", carBrandParam as Serializable)
            context.setResult(102, intent)
            context.finish()
        }


    }

    /**
     *   获取车型车系回调
     */
    fun setChex(type: String, list: List<CarXiParam>) {
        var temlist = ArrayList<X>()
        for (item in list) {
            temlist.addAll(item.list)
        }
        when (type) {
            "3" -> cexiList.postValue(temlist)
            "5" -> carXingList.postValue(temlist)
        }
    }

    fun initData(i: Int) {
        when (i) {
            1 -> {
                chooseCarRepository.getCar("3", carBrandParam.brand, this@ChooseCarViewModel::setChex)
            }
            2 -> {
                chooseCarRepository.getCar("5", carBrandParam.series, this@ChooseCarViewModel::setChex)
            }
        }
    }

    /**
     * 只选择品牌
     */
    fun setIsbrand(Isbrand: Boolean) {
        this.Isbrand = Isbrand
    }

    /**
     * 只选择到车系
     */
    fun setIsseries(booleanExtra: Boolean) {
        Isseries = booleanExtra
    }

    fun setLevel(v: Int) {
        level = v
    }
}
